#!/bin/bash
rm -rf /etc/auth_system

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'

# 检查 root 权限
check_root() {
    if [ "$(id -u)" -ne 0 ]; then
        echo -e "${RED}错误：请使用 root 用户或通过 sudo 运行此脚本${NC}"
        exit 1
    fi
}

# 分类应用菜单
declare -A CATEGORIES=(
    ["1-安全防护"]="fail2ban IP拦截和防护系统 sshkey ssh密钥"
    ["2-办公系统"]="gouguoa OA办公系统"
    ["3-网络工具"]="ikuai 路由工具*注意会改变启动方式* xui xui面板 3xui 3xui面板 gost gost面板 openport 开放端口管理 ReverseProxy nginx反向代理"
    ["4-证书管理"]="generate_cert 自签证书生成"
)

# 创建安装函数
install_app() {
    case $1 in
    #1、安全防护
    "fail2ban") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/linux-install-fail2ban.sh)" ;;
    "sshkey") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/linux-install-sshkey.sh)" ;;
       
    #2、办公系统
    "gouguoa") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/linux-install-oa.sh)" ;;
            
    #3、网络工具 

    "ikuai") bash -c "$(curl -sSLf https://gitee.com/banban1000/ik/raw/master/linux-install-ik.sh)" ;;
    "xui") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/linux-install-xui.sh)" ;;
    "3xui") bash -c "$(curl -fsSL https://gitee.com/banban1000/docker/raw/master/linux/linux-install-3xui.sh)" ;;
    "gost") wget --no-check-certificate -O gost.sh https://raw.githubusercontent.com/mfkd1000/xui/main/gost.sh &&  chmod +x gost.sh  &&  bash gost.sh ;;      
    "openport") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/linux-install-portctl.sh)" ;;    
    "openport") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/linux-install-portctl.sh)" ;;   

    #4、证书管理  
    "ReverseProxy") bash -c "$(curl -sSLf https://gitee.com/banban1000/docker/raw/master/linux/linux-install-nginx-ReverseProxy.sh)" ;;    
                
    #、其它
        *)
            echo -e "${RED}暂不支持安装 $1${NC}"
            ;;
    esac
}

# 显示菜单
show_menu() {
    clear
    echo -e "${PURPLE}============================================${NC}"
    echo -e "${PURPLE}          Linux 应用一键安装脚本          ${NC}"
    echo -e "${PURPLE}============================================${NC}"
    echo -e "${YELLOW}请选择要安装的应用 (输入分类编号+应用编号，如101选择第1分类第1个应用):${NC}"
    echo -e "${PURPLE}--------------------------------------------${NC}"
    
    declare -gA APP_MAP  # 声明为全局变量
    
    # 按分类名称排序
    local sorted_categories
    IFS=$'\n' sorted_categories=($(sort <<<"${!CATEGORIES[*]}"))
    
    for category in "${sorted_categories[@]}"; do
        # 提取分类编号和名称
        category_num=${category%%-*}
        category_name=${category#*-}
        
        echo -e "${BLUE}${category_num}. ${category_name}${NC}"
        
        IFS=' ' read -ra APPS <<< "${CATEGORIES[$category]}"
        local app_index=0
        
        # 计算最大名称长度用于对齐
        local max_len=0
        for ((i=0; i<${#APPS[@]}; i+=2)); do
            [ ${#APPS[$i]} -gt $max_len ] && max_len=${#APPS[$i]}
        done
        
        # 显示应用
        for ((i=0; i<${#APPS[@]}; i+=2)); do
            app_index=$((i/2+1))
            APP_MAP["${category_num}$(printf "%02d" $app_index)"]="${APPS[$i]}"
            printf "    ${GREEN}%02d${NC} %-${max_len}s ${YELLOW}- %s${NC}\n" \
                   "$app_index" "${APPS[$i]}" "${APPS[$i+1]}"
        done
        echo
    done
    
    echo -e "${PURPLE}--------------------------------------------${NC}"
    echo -e "${YELLOW}输入 0 退出脚本${NC}"
    echo -e "${PURPLE}--------------------------------------------${NC}"
}

# 主循环
main() {
    check_root
    
    while true; do
        show_menu
        read -p "请输入选择 (如101选择第1分类第1个应用), 或0退出: " CHOICE
        
        if [[ "$CHOICE" == "0" ]]; then
            echo -e "${YELLOW}退出脚本。${NC}"
            exit 0
        elif [[ "$CHOICE" =~ ^[0-9]{3}$ ]]; then
            # 提取分类号(第一位)和应用号(后两位)
            category_num=${CHOICE:0:1}
            app_num=${CHOICE:1:2}
            
            # 去除前导零
            app_num=$((10#$app_num))
            
            # 构建选择键(分类号+两位数应用号)
            SELECT_KEY="${category_num}$(printf "%02d" $app_num)"
            
            SELECTED_APP="${APP_MAP[$SELECT_KEY]}"
            if [[ -n "$SELECTED_APP" ]]; then
                # 获取应用描述
                for category in "${!CATEGORIES[@]}"; do
                    if [[ "${category:0:1}" == "$category_num" ]]; then
                        IFS=' ' read -ra APPS <<< "${CATEGORIES[$category]}"
                        for ((i=0; i<${#APPS[@]}; i+=2)); do
                            if [[ "${APPS[$i]}" == "$SELECTED_APP" ]]; then
                                APP_DESC="${APPS[$i+1]}"
                                break 2
                            fi
                        done
                    fi
                done
                
                echo -e "${GREEN}您选择了: ${SELECTED_APP} - ${APP_DESC}${NC}"
                # 修改这里：默认确认安装，按N取消安装
                read -p "确认安装 $SELECTED_APP? [Y/n]: " CONFIRM
                
                # 修改这里：默认确认（空值或Y/y确认，N/n取消）
                if [[ -z "$CONFIRM" || "$CONFIRM" =~ [yY] ]]; then
                    echo -e "${CYAN}开始安装 ${SELECTED_APP}...${NC}"
                    install_app "$SELECTED_APP"
                    read -p "安装完成，按回车键继续..."
                elif [[ "$CONFIRM" =~ [nN] ]]; then
                    echo -e "${YELLOW}已取消安装${NC}"
                    sleep 1
                else
                    # 如果输入了非Y/y/N/n的字符，也默认确认安装
                    echo -e "${CYAN}开始安装 ${SELECTED_APP}...${NC}"
                    install_app "$SELECTED_APP"
                    read -p "安装完成，按回车键继续..."
                fi
            else
                echo -e "${RED}无效选择，分类${category_num}的应用${app_num}不存在${NC}"
                sleep 2
            fi
        else
            echo -e "${RED}无效格式，请使用三位数格式 (如101表示第1分类第1个应用)${NC}"
            sleep 2
        fi
    done
}

main